package com.example.wechatbusiness.config;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Getter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Result<T> {

    private String message;
    private int status;
    private T result;

    public static Result ok(){
        Result<Object> result = new Result<>();
        result.status=200;
        return result;
    }

    public static Result defeate(){
        Result<Object> result = new Result<>();
        result.status=400;
        result.message="未知错误";
        return result;
    }

    public static Result ok(String message){
        Result<Object> result = new Result<>();
        result.status=200;
        result.message=message;
        return result;
    }

    public static Result ok(Integer status,String message,Object o){
        Result<Object> result = new Result<>();
        result.status=200;
        result.message=message;
        result.result=o;
        return result;
    }

    public Result(ResultBuilder<T> builder) {
        this.message = builder.message;
        this.status = builder.status;
        this.result = builder.result;
    }

    public static <T> ResultBuilder<T> builder() {
        return new ResultBuilder<>(200, "");
    }

    public static <T> ResultBuilder<T> builder(int code, String message) {
        return new ResultBuilder<>(code, message);
    }

    public static class ResultBuilder<T> {
        private String message;
        private int status;
        private T result;

        public ResultBuilder(int status, String message) {
            this.message = message;
            this.status = status;
        }

        public ResultBuilder<T> status(int status) {
            this.status = status;
            return this;
        }

        public ResultBuilder<T> message(String message) {
            this.message = message;
            return this;
        }

        public ResultBuilder<T> result(T result) {
            this.result = result;
            return this;
        }
        public Result<T> build() {
            return new Result<>(this);
        }
    }

}
